package com.ss.android.image.loader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.io.NetHackDbg;
import com.bytedance.frameworks.baselib.network.http.util.d;
import com.bytedance.frameworks.baselib.network.http.util.TaskInfo;
import com.ss.android.common.load.AsyncLoader;
import com.ss.android.image.BaseImageManager;
import com.ss.android.image.m;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;

import pl.droidsonroids.gif.GifDrawable;

public final class LargeImageLoader {
    public interface a {
        void a(String arg1, Object arg2);
    }

    public AsyncLoader a;
    public final a b;
    public boolean c;
    private AsyncLoader.LoaderProxy d;
    private Context e;
    private TaskInfo f;
    private d g;
    private BaseImageManager h;

    public LargeImageLoader(Context arg8, TaskInfo arg9, BaseImageManager arg10, d arg11, a arg12) {
        this(arg8, arg9, arg10, arg11, arg12, (byte) 0);
    }

    private LargeImageLoader(Context arg5, TaskInfo arg6, BaseImageManager arg7, d arg8, a arg9, byte arg10) {
        this.e = arg5.getApplicationContext();
        this.f = arg6;
        this.h = arg7;
        this.b = arg9;
        this.g = arg8;
        this.d = new com.ss.android.image.loader.g(this);
        this.a = new AsyncLoader(16, 2, this.d);
        this.c = true;
    }

    public final void a() {
        this.c = true;
        this.a.resume();
    }

    private static Bitmap a(String arg5) {
        Bitmap v0 = null;
        int v1 = -1;
        try {
            Bitmap v1_3 = android.support.design.a.a(arg5, v1, false);
            if (v1_3 == null) {
                return v0;
            }

            ByteArrayOutputStream v2 = new ByteArrayOutputStream();
            v1_3.compress(Bitmap.CompressFormat.JPEG, 80, ((OutputStream) v2));
            v1_3.recycle();
            byte[] v1_4 = v2.toByteArray();
            v2.close();
            BitmapFactory.Options v2_1 = new BitmapFactory.Options();
            v2_1.inPurgeable = true;
            v2_1.inPreferredConfig = Bitmap.Config.RGB_565;
            v0 = BitmapFactory.decodeByteArray(v1_4, 0, v1_4.length, v2_1);
        } catch (Exception v1_1) {
            Logger.w("LargeImageLoader", "loadLocal exception " + v1_1);
        } catch (Throwable v1_2) {
            Logger.d("LargeImageLoader", "loadLocal oom:" + v1_2);
        }

        return v0;
    }

    final Object a(String arg13, String arg14, String arg15) {
        Object v0_5;
        String v1_1;
        String v11;
        try {
            v11 = this.h.getExternalCacheFile(arg13);
            String v10 = this.h.getInternalCacheFile(arg13);
            File v1 = new File(v11);
            File v2 = new File(v11);
            boolean v0_1 = false;
            if ((v1.isFile()) || (v2.isFile())) {
                v0_1 = true;
            }

            if (!v0_1) {
                v0_1 = m.a().downloadImage(this.e, -1, arg14, arg15, this.h.getExternalImageSaveDir(arg13), this.h.getInternalImageSaveDir(arg13), BaseImageManager.getCacheFileName(arg13), this.g, this.f);
            }

            if (!v0_1) {
                v0_5 = null;
                return v0_5;
            }

            File v0_2 = new File(v11);
            if (!v0_2.isFile()) {
                v0_2 = new File(v10);
                v1_1 = v10;
            } else {
                v1_1 = v11;
            }
            if ((v0_2.isFile()) && (NetHackDbg.a(v0_2))) {
                v0_5 = b(v1_1);
                return v0_5;
            }

            v0_5 = a(v1_1);
            return v0_5;
        } catch (Throwable v0) {
            Logger.d("LargeImageLoader", "loadImage exception " + v0);
            return null;
        }
    }

    public final void b() {
        this.c = false;
        this.a.stop();
        if (this.f != null) {
            this.f.a = true;
        }
    }

    private static GifDrawable b(String arg4) {
        GifDrawable v0_1;
        try {
            v0_1 = new GifDrawable(arg4);
        } catch (Throwable v0) {
            Logger.d("LargeImageLoader", "loadLocalGif exception " + v0);
            v0_1 = null;
        }

        return v0_1;
    }
}

